/*
 * @(#) PublicItemServiceImpl.java 2011-9-28
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.publiceva.business.service.impl;

import java.util.Date;

import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.business.entity.db.JxkhPublicItemEntity;
import cn.strong.gweva.common.business.service.impl.AbstractServiceImpl;
import cn.strong.gweva.common.util.Constants;
import cn.strong.gweva.common.util.Constants4Method;
import cn.strong.gweva.publiceva.business.entity.PublicItemEntity;
import cn.strong.gweva.publiceva.business.service.PublicItemService;

/**
 * 公共考核项目。
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-9-28
 */
public class PublicItemServiceImpl extends AbstractServiceImpl implements PublicItemService
{

	/**
	 * 公共考核项目分页查询.
	 * 
	 * @param pagerEntity
	 * @param publicItemSearchEntity
	 * @return 公共考核项目分页信息
	 * @throws Exception 异常
	 */
	public PagerEntity searchPublicItemPage(PagerEntity pagerEntity,
			JxkhPublicItemEntity publicItemSearchEntity) throws Exception
	{
		return this.getCommonDao().searchPager(pagerEntity,
				Constants4Method.PUBLICEVA_SEARCH_PUBLIC_ITEM_LIST, publicItemSearchEntity);
	}

	/**
	 * 新增公共考核项目.
	 * 
	 * @param publicItemEntity 公共考核项目管理entity
	 * @throws Exception 异常
	 */
	public void insert(PublicItemEntity publicItemEntity) throws Exception
	{
		// 公共考核项目
		publicItemEntity.getPublicItem().setDelFlag(Constants.DEL_FLAG_NO);
		publicItemEntity.getPublicItem().setUpdateTime(new Date());
		Integer itemId = (Integer)this.getCommonDao().insert(
				Constants4Method.PUBLICEVA_INSERT_PUBLIC_ITEM, publicItemEntity.getPublicItem());

		if (publicItemEntity.getPublicRuleList() != null && publicItemEntity.getPublicRuleList().size() > 0)
		{
			for (int i = publicItemEntity.getPublicRuleList().size() - 1; i >= 0; i--)
			{
				if (publicItemEntity.getPublicRuleList().get(i) == null)
				{
					publicItemEntity.getPublicRuleList().remove(i);
					continue;
				}

				publicItemEntity.getPublicRuleList().get(i).setItemId(itemId);
				publicItemEntity.getPublicRuleList().get(i).setDelFlag(Constants.DEL_FLAG_NO);
				publicItemEntity.getPublicRuleList().get(i).setUpdateTime(new Date());
				publicItemEntity.getPublicRuleList().get(i).setUpdateUserId(
						publicItemEntity.getPublicItem().getUpdateUserId());
			}

			this.getCommonDao().insertBatch(Constants4Method.PUBLICEVA_INSERT_PUBLIC_RULE,
					publicItemEntity.getPublicRuleList());
		}
	}

	/**
	 * 修改公共考核项目.
	 * 
	 * @param publicItemEntity 公共考核项目管理entity
	 * @throws Exception 异常
	 */
	public void updatePublicItem(PublicItemEntity publicItemEntity) throws Exception
	{
		// 更新公共考核项目
		publicItemEntity.getPublicItem().setDelFlag(Constants.DEL_FLAG_NO);
		publicItemEntity.getPublicItem().setUpdateTime(new Date());
		this.getCommonDao().update(Constants4Method.PUBLICEVA_UPDATE_PUBLIC_ITEM,
				publicItemEntity.getPublicItem());

		// 逻辑删除公共考核规则
		publicItemEntity.getPublicItem().setDelFlag(Constants.DEL_FLAG_YES);
		publicItemEntity.getPublicItem().setUpdateTime(new Date());
		this.getCommonDao().update(
				Constants4Method.PUBLICEVA_LOGIC_DELETE_PUBLIC_RULE_BY_PUBLIC_ITEM,
				publicItemEntity.getPublicItem());

		// 新增
		if (publicItemEntity.getPublicRuleList() != null && publicItemEntity.getPublicRuleList().size() > 0)
		{
			for (int i = publicItemEntity.getPublicRuleList().size() - 1; i >= 0; i--)
			{
				if (publicItemEntity.getPublicRuleList().get(i) == null)
				{
					publicItemEntity.getPublicRuleList().remove(i);
					continue;
				}

				publicItemEntity.getPublicRuleList().get(i).setItemId(
						publicItemEntity.getPublicItem().getItemId());
				publicItemEntity.getPublicRuleList().get(i).setDelFlag(Constants.DEL_FLAG_NO);
				publicItemEntity.getPublicRuleList().get(i).setUpdateTime(new Date());
				publicItemEntity.getPublicRuleList().get(i).setUpdateUserId(
						publicItemEntity.getPublicItem().getUpdateUserId());
			}

			this.getCommonDao().insertBatch(Constants4Method.PUBLICEVA_INSERT_PUBLIC_RULE,
					publicItemEntity.getPublicRuleList());
		}
	}

	/**
	 * 逻辑删除公共考核项目.
	 * 
	 * @param publicItemEntity 公共考核项目管理entity
	 * @throws Exception 异常
	 */
	public void logicDeletePublicItem(PublicItemEntity publicItemEntity) throws Exception
	{
		// 逻辑删除公共考核项目
		publicItemEntity.getPublicItem().setDelFlag(Constants.DEL_FLAG_YES);
		publicItemEntity.getPublicItem().setUpdateTime(new Date());
		this.getCommonDao().update(Constants4Method.PUBLICEVA_LOGIC_DELETE_PUBLIC_ITEM,
				publicItemEntity.getPublicItem());

		// 逻辑删除公共考核规则
		this.getCommonDao().update(
				Constants4Method.PUBLICEVA_LOGIC_DELETE_PUBLIC_RULE_BY_PUBLIC_ITEM,
				publicItemEntity.getPublicItem());
	}

	/**
	 * 通过公共考核项目ID，删除公共考核项目。
	 * 
	 * @param itemId 公共考核项目ID
	 * @throws Exception 异常
	 */
	public void deletePublicItemByItemId(Integer itemId) throws Exception
	{
		// 删除公共考核项目
		this.getCommonDao().update(Constants4Method.PUBLICEVA_DELETE_PUBLIC_ITEM_BY_ID, itemId);

		// 删除公共考核规则
		this.getCommonDao().update(Constants4Method.PUBLICEVA_DELETE_PUBLIC_RULE_BY_ITEM_ID, itemId);
	}

	/**
	 * 查询公共考核项目详细信息.
	 * 
	 * @param itemId 公共考核项目ID
	 * @return 公共考核项目详细信息
	 * @throws Exception 异常
	 */
	@SuppressWarnings("unchecked")
	public PublicItemEntity searchPublicItemById(Integer itemId) throws Exception
	{
		PublicItemEntity publicItemEntity = new PublicItemEntity();

		publicItemEntity.setPublicItem((JxkhPublicItemEntity)this.getCommonDao().searchObj(
				Constants4Method.PUBLICEVA_SEARCH_PUBLIC_ITEM_BY_ID, itemId));
		publicItemEntity.setPublicRuleList(this.getCommonDao().searchList(
				Constants4Method.PUBLICEVA_SEARCH_PUBLIC_RULE_BY_ITEM_ID,
				publicItemEntity.getPublicItem().getItemId()));

		return publicItemEntity;
	}

}
